export default {
  namespaced:true,
  state(){
    return{
      list:[
        {
          name: "诸葛亮",
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: "蔡文姬",
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: "妲己",
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: "鲁班",
          price: 2200,
          num: 1,
          checked: false,
        },
      ]
    }
  },
 //相当于计算属性 合计
  getters:{
    All(state){
      //用filter 返回是个数组
      const arr=state.list.filter(item=>item.checked)
      return arr.reduce((sum,item)=>sum+item.price*item.num,0)
    },
  },
  //修改数据 数量加加减减 需要传索引值过来
  mutations:{
    updateCount(state,payload){
      state.list[payload.index].num=payload.num//payload.num即newNum
    },
    del(state,payload){
      state.list.splice(payload,1)//此时payload是索引值
    },
    changeChecked(state,payload){
      state.list[payload].checked=!state.list[payload].checked//单选双向绑定 改变状态时取反
    }
  },
  actions:{

  }

}